//
//  areaEncyclopediasVC.m
//  HappyStore
//
//  Created by 陈锋 on 16/5/23.
//  Copyright © 2016年 LLJ. All rights reserved.
//

#import "areaEncyclopediasVC.h"
#import "areaCell.h"

// 屏幕

@interface areaEncyclopediasVC ()<UITableViewDelegate,UITableViewDataSource>{
    CGPoint point;
}
@property (weak, nonatomic) IBOutlet UIWebView *webview;
@property (strong, nonatomic) UIButton *topBtn;
@property (strong, nonatomic) NSDictionary *dataDict;

@property (nonatomic, strong) UIButton *menuView;
@property (nonatomic, strong) UITableView *menuCategoryView;
@property (strong, nonatomic) NSArray *labelArr;

@end

static NSString *areacell = @"areaCell";
@implementation areaEncyclopediasVC

#pragma mark - 初始化
-(NSArray *)labelArr{
    if (!_labelArr) {
        _labelArr = @[@"地区概况",@"建制沿革",@"行政区划",@"地理环境",@"自然资源",@"人口民族",@"交通优势",@"经济概况",@"社会事业",@"历史文化",@"风景名胜",@"著名人物"];
    }
    return _labelArr;
}
-(NSDictionary *)dataDict{
    if (!_dataDict) {
        _dataDict = [NSDictionary dictionary];
    }
    return _dataDict;
}

- (UIButton *)menuView
{
    if (!_menuView) {
        self.menuView = [[UIButton alloc] init];
    }
    return _menuView;
}
-(void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:YES];
    
    [self sendRequest];

}

- (void)viewDidLoad {
    [super viewDidLoad];
  
    UILabel *titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 190, 44)];
    titleLabel.backgroundColor = [UIColor clearColor];
    titleLabel.font = [UIFont systemFontOfSize:19];
    titleLabel.text = @"地区百科";
    titleLabel.textColor = [UIColor whiteColor];
    titleLabel.textAlignment = NSTextAlignmentCenter;
    self.navigationItem.titleView = titleLabel;
    
    self.webview.opaque = NO;
    self.webview.backgroundColor = [UIColor clearColor];
    self.webview.scrollView.backgroundColor = [UIColor whiteColor];
    
    UIButton *btn = [[UIButton alloc]initWithFrame:CGRectMake(screenW-55, screenH/3*2, 45, 45)];
    btn.layer.masksToBounds = YES;
    btn.layer.cornerRadius = btn.bounds.size.width / 2.0;
    [btn setTitle:@"" forState:UIControlStateNormal];
    [btn setTitle:@"" forState:UIControlEventTouchDown];//移动按钮
    [btn setBackgroundColor:[UIColor clearColor]];
    [btn addTarget:self action:@selector(shareSDKwithBtn:) forControlEvents:UIControlEventTouchUpInside];
    [btn addTarget:self action:@selector(dragMoving:withEvent: )forControlEvents: UIControlEventTouchDragInside];
    
    [btn addTarget:self action:@selector(pointBegain:) forControlEvents:UIControlEventTouchDown];
    
    [btn setImage:[UIImage imageNamed:@"组-111"] forState:UIControlStateNormal];
    btn.imageView.contentMode = UIViewContentModeCenter;
    self.topBtn = btn;
//    [self.webview addSubview:btn];
    
    // 设置返回按钮
    UIBarButtonItem *leftItem = [UIBarButtonItem itemWithTarget:self action:@selector(leftBarButtonItemClick) image:@"Back" highImage:@"Back"];
    self.navigationItem.leftBarButtonItem = leftItem;
    
    UIBarButtonItem *rightItem = [UIBarButtonItem itemWithTarget:self action:@selector(rightBarButtonItemClick) image:@"组-59" highImage:@"组-59"];
//    self.navigationItem.rightBarButtonItem = rightItem;
    
    //开启侧滑
    self.navigationController.interactivePopGestureRecognizer.delegate = (id)self;

}
-(void)leftBarButtonItemClick{
    
    [self.navigationController popViewControllerAnimated:YES];
}

-(void)rightBarButtonItemClick{
    
    // 侧滑菜单
    // 只执行一次
//    static dispatch_once_t justDoOnce;
//    dispatch_once(&justDoOnce, ^{
    
        self.menuView.frame = self.view.bounds;
        self.menuView.height = [UIScreen mainScreen].bounds.size.height;
        self.menuView.backgroundColor = [UIColor colorWithRed:0.8 green:0.8 blue:0.8 alpha:0.6];
        
        // 分类侧栏栏
        self.menuCategoryView = [[UITableView alloc]init];
        self.menuCategoryView.frame = CGRectMake(0, 64, screenW * (380.0 / 640.0), screenH - 64);
        self.menuCategoryView.x = self.menuCategoryView.width * 2;
        self.menuCategoryView.backgroundColor = [UIColor whiteColor];
        // 设置侧栏里面的内容
        [self setupMenCategoryContent];

//    });

    [self.view addSubview:self.menuView];
    // 在蒙版上添加分类栏
    self.menuCategoryView.x =  self.menuCategoryView.width * 2;
    [self.menuView addSubview:self.menuCategoryView];
    
    // 分类栏从右边画出
    [UIView animateWithDuration:0.2 animations:^{
        
        self.menuCategoryView.x = screenW - self.menuCategoryView.width;
    }];
    
    [self.menuView addTarget:self action:@selector(menuViewBtnClick) forControlEvents:UIControlEventTouchUpInside];
    
}
-(void)setupMenCategoryContent{
    
    self.menuCategoryView.delegate = self;
    self.menuCategoryView.dataSource = self;
    self.menuCategoryView.showsVerticalScrollIndicator = NO;
    self.menuCategoryView.showsHorizontalScrollIndicator = NO;
    UINib * nib = [UINib nibWithNibName:@"areaCell" bundle:nil];
    [self.menuCategoryView registerNib:nib forCellReuseIdentifier:areacell];
    
}

#pragma mark - tableview datasource
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.labelArr.count;
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 60;
}

-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    return 1;
}

-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return 1;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
//    UINib *nib = [UINib nibWithNibName:@"goveCell" bundle:nil];
//    [self.collectionview registerNib:nib forCellWithReuseIdentifier:identifier];
    
    areaCell * cell = [self.menuCategoryView dequeueReusableCellWithIdentifier:areacell];
    if (!cell) {
        cell = [[areaCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:areacell];
    }
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    cell.accessoryType = UITableViewCellAccessoryNone;
    cell.backgroundColor = [UIColor colorWithRed:67.0/255.0 green:177.0/255.0 blue:241.0/255.0 alpha:1];
    cell.label.text = self.labelArr[indexPath.row];

    cell.x = 10;
    
    return cell;
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    NSArray *arr = @[@"1",@"2",@"3",@"4",@"5",@"6",@"7",@"8",@"9",@"10",@"11",@"12"];
    self.type = arr[indexPath.row];

    [self sendRequest];
}
#pragma mark - 按钮方法
-(void)shareSDKwithBtn: (UIButton*)btn{
    
    if (point.x == btn.center.x && point.y == btn.center.y) {
        btn.selected =! btn.selected;
        CGPoint offset = self.webview.scrollView.contentOffset;
        offset.y = 10;
        [self.webview.scrollView setContentOffset:offset animated:YES];
    }
    
    
}
/*
 记录用户点击时按钮坐标
 */
-(void)pointBegain: (UIButton*)btn{
    point = btn.center;
}
/*
 移动按钮方法
 */
- (void) dragMoving: (UIControl *) c withEvent:ev
{
    c.center = [[[ev allTouches] anyObject] locationInView:self.view];
}
- (void)menuViewBtnClick
{
    
        [UIView animateWithDuration:1 animations:^{
            
            self.menuCategoryView.x = (- self.menuCategoryView.width);
            
        }];
    
     [self.menuView removeFromSuperview];
    
}
#pragma mark - 请求数据
-(void)sendRequest{
    
    NSMutableDictionary * dic = [NSMutableDictionary dictionary];
    dic[@"userId"] = self.userId;
    //dic[@"type"] = self.type;
    [UIView animateWithDuration:1 animations:^{
        
        self.menuCategoryView.x = (- self.menuCategoryView.width);
        
    }];
    
    [self.menuView removeFromSuperview];
    //goveAreaWiki_app_myHome.im
    [LSHttpTool sendHttpData:@"goveAreaWikiBaiduUrl_app_myHome.im" params:dic success:^(id responseObject) {
        NSDictionary *dicInfo = [NSDictionary dictionaryWithDictionary:responseObject];
        _dataDict = dicInfo[@"data"];
        
        NSURL *url = [NSURL URLWithString:self.dataDict[@"baiduUrl"]];
        NSURLRequest *urlRequest = [NSURLRequest requestWithURL:url];
        self.webview.scalesPageToFit = YES;
        [self.webview loadRequest:urlRequest];


    } failure:^(NSError *error) {

       [self showSingleIconBubble:@"网络连接错误"];
    
    }];
}

@end
